package com.lovo.atm.dao.impl;

import com.lovo.atm.bean.AtmUserBean;
import com.lovo.atm.bean.MessageBean;
import com.lovo.atm.dao.IMessgeDao;

import java.sql.SQLException;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;

public class IMessgeDaoImpl extends BaseDao implements IMessgeDao {
    private List<MessageBean> list = new ArrayList<>();

    private MessageBean bean = null;

    @Override
    public List<MessageBean> findDeal(String code) {
        setCon();
        try {
            ps = con.prepareStatement("select * from t_atmMsg where fk_code=? ");
            ps.setObject(1, code);
            rs = ps.executeQuery();
            while (rs.next()) {
                list.add(cv());
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return list;
    }
    @Override
    public void addDeal(MessageBean msBean) {
        try {
            this.setCon();
            ps = con.prepareStatement("insert into t_atmMsg(fk_code,m_msg,m_date,m_count)" +
                    "values (?,?,?,?)");
            ps.setObject(1, msBean.getName());
            ps.setObject(2, msBean.getMas());
            ps.setObject(3, msBean.getDate());
            ps.setObject(4, msBean.getMsCount());
            ps.executeUpdate();
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            close();
        }
    }

    public MessageBean cv() throws SQLException {
        MessageBean user = new MessageBean();
        user.setId(rs.getInt("pk_atmMsgId"));
        user.setName(rs.getString("fk_code"));
        user.setMas(rs.getString("m_msg"));
        user.setMsCount(rs.getInt("m_count"));
        user.setDate(LocalDate.parse(rs.getString("m_date")));
        return user;
    }
}
